package com.ncloud.act.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.act.common.ActDataHelper;
import com.ncloud.act.mapper.BsCycleMapper;
import com.ncloud.act.mapper.ChnlAdjustBillMapper;
import com.ncloud.act.mapper.ChnlBillMapper;
import com.ncloud.act.mapper.ChnlDetailBillMapper;
import com.ncloud.act.mapper.ChnlMapper;
import com.ncloud.act.mapper.ChnlSalesMapper;
import com.ncloud.act.service.ChnlBillService;
import com.ncloud.act.entity.BsCycle;
import com.ncloud.common.exception.UserException;

@Service
public class ChnlBillServiceImpl implements ChnlBillService{
	private static final Logger logger = LogManager.getLogger(ChnlBillServiceImpl.class);
//	@Resource
//	private ChnlSalesMapper chnlSalesMapper;
	@Resource
	private ChnlBillMapper chnlBillMapper;
	@Resource
	private ChnlDetailBillMapper chnlDetailBillMapper;
	@Resource
	private ChnlMapper chnlMapper;
	@Resource
	private BsCycleMapper bsCycleMapper;
	@Resource
	private ChnlAdjustBillMapper chnlAdjustBillMapper;
	@Override
	public JSONArray getChnlBillInfo(JSONObject json) throws ParseException {
		Integer user_id = 0;
		//Long chnl_id = 0l;
		String start_time="";
		String end_time="";
		if(json.containsKey("userID"))
		{
			user_id=json.getInteger("userID");
		}else{
			throw new UserException("4000", "没有用户编码");
		}
		if(json.containsKey("start_date"))
		{
			start_time=json.getString("start_date");
		}else{
			throw new UserException("4000", "没有开始日期");
		}
		start_time=start_time+" 00:00:00";
		if(json.containsKey("end_date"))
		{
			end_time=json.getString("end_date");
		}else{
			throw new UserException("4000", "没有结束日期");
		}
		end_time=end_time+" 23:59:59";
		//user_id=33l;//test
		/*List<ChnlSales> ChnlSales = chnlSalesMapper.getChnlSalesByUserID(user_id);
		if(ChnlSales!=null && ChnlSales.size()>0){
			chnl_id=ChnlSales.get(0).getChnl_id();
		}*/
		List<JSONObject> chnlBills = chnlBillMapper.getChnlBillInfo(user_id, start_time, end_time);
		for(int i=0;i<chnlBills.size();i++){
			JSONObject chnlBill=chnlBills.get(i);
			//上月调账金额
			//chnlBill.put("adjust_fee", 0);
			String[] dateStr = getDateString(chnlBill.getInteger("cycle_id"));
			chnlBill.put("start_date", dateStr[0]);
			chnlBill.put("end_date", dateStr[1]);
		}
		JSONArray results=JSON.parseArray(JSON.toJSONString(chnlBills));
		ActDataHelper.div100(results, new String[] {"fee","balance","adjust_fee","current_adjust_fee"});
		return results;
	}
	private String[] getDateString(Integer cycle_id) {
		int year = cycle_id/100;
		int month = cycle_id%100;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cale = Calendar.getInstance();
		cale.set(Calendar.YEAR, year);
		cale.set(Calendar.MONTH, month-1);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		String firstday = df.format(cale.getTime());
		cale.add(Calendar.MONTH, 1);
		cale.add(Calendar.DAY_OF_MONTH, -1);
		String lastday = df.format(cale.getTime());
		return new String[]{firstday,lastday};
	}
	@Override
	public JSONObject getChnlDetailBillInfo(JSONObject json) throws ParseException {
		JSONObject result = new JSONObject();
		Integer user_id = 0;
		//Long chnl_id = 0l;
		Long total_id= 0l;
		Long total_chnl_id= 0l;
		if(json.containsKey("userID"))
		{
			user_id=json.getInteger("userID");
		}else{
			throw new UserException("4000", "没有用户编码");
		}
		if(json.containsKey("total_id"))
		{
			total_id=json.getLong("total_id");
		}else{
			throw new UserException("4000", "没有结算单号");
		}
		if(json.containsKey("total_chnl_id"))
		{
			total_chnl_id=json.getLong("total_chnl_id");
		}else{
			throw new UserException("4000", "没有结算渠道编码");
		}
		//user_id=33l;//test
		/*List<ChnlSales> ChnlSales = chnlSalesMapper.getChnlSalesByUserID(user_id);
		if(ChnlSales!=null && ChnlSales.size()>0){
			chnl_id=ChnlSales.get(0).getChnl_id();
		}*/
		//结算单明细
		List<JSONObject> chnlDetailBills = chnlDetailBillMapper.getChnlDetailBillByTotalID(total_id,user_id);
		JSONArray detailBills=JSON.parseArray(JSON.toJSONString(chnlDetailBills));
		ActDataHelper.div100(detailBills, new String[] {"fee","balance"});
		
		result.put("total_id",String.valueOf(total_id));
		result.put("detail_bills", detailBills);
		//云网渠道信息
		List<JSONObject> chnl1 = chnlMapper.getChnlByChnlLevel("0");
		if(chnl1!=null&&chnl1.size()>0){
			result.put("chnl1", chnl1.get(0));
		}
		//结算单渠道信息
		List<JSONObject> chnl2 = chnlMapper.getChnlByChnlID(total_chnl_id);
		if(chnl2!=null&&chnl2.size()>0){
			result.put("chnl2", chnl2.get(0));
		}
		//最大出账账单账期
		Integer bill_cycle_id=0;
		List<BsCycle> maxBillCycle = bsCycleMapper.getMaxOpenCycle();
		if (maxBillCycle.size()>0){
			bill_cycle_id=maxBillCycle.get(0).getCycle_id();
		}
		//上月调账信息
		List<JSONObject> chnlAdjustBill = chnlAdjustBillMapper.getAdjustFeeByChnlID(total_chnl_id, bill_cycle_id, "1");
		if (chnlAdjustBill!=null && chnlAdjustBill.size()>0){
			JSONArray ja=JSON.parseArray(JSON.toJSONString(chnlAdjustBill));
			ActDataHelper.div100(ja, new String[] {"adjust_fee"});
			result.put("adjust_fee",ja.getJSONObject(0).getString("adjust_fee"));
		}else{
			result.put("adjust_fee", 0l);
		}
		return result;
	}
}
